package com.lianniu.mgr.teaOrder.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.teaAddr.dao.TeaUserAddrDao;
import com.lianniu.mgr.teaAddr.model.TeaUserAddr;
import com.lianniu.mgr.teaInfo.model.TeaFrontusers;
import com.lianniu.mgr.teaInfo.service.ITeaInfoService;
import com.lianniu.mgr.teaOrder.dao.TeaOrderDao;
import com.lianniu.mgr.teaOrder.model.TeaOrder;
import com.lianniu.mgr.teaOrder.service.ITeaOrderService;
import com.lianniu.mgr.teaProduct.dao.TeaProductDao;
import com.lianniu.mgr.teaProduct.model.TeaProduct;
import com.lianniu.mgr.util.DateUtil;
import com.lianniu.mgr.util.TeaContant;
import com.lianniu.mgr.util.UUIDUtil;



@Service
@Transactional
public class TeaOrderServiceImpl implements ITeaOrderService {

	private final static Logger logger = Logger.getLogger(TeaOrderServiceImpl.class);
	
	@Autowired
	private TeaProductDao teaProductDao;
	
	@Autowired
	private TeaUserAddrDao teaUserAddrDao;
	
	@Autowired
	private TeaOrderDao teaOrderDao;
	
	@Autowired
	private ITeaInfoService teaInfoService;
	
	/**
	 * 用户点击购买后返回前台生成订单所需的信息
	 */
	@SuppressWarnings("unchecked")
	public BaseResult getOrderInfoBefore(String userId, String productId) {
		logger.info("TeaOrderServiceImpl getOrderInfoBefore start");
		TeaProduct teaProduct = teaProductDao.selectProOeder(Long.valueOf(productId));
		TeaUserAddr teaUserAddr =  teaUserAddrDao.selectDefaultAddr(userId);
		Map result = new HashMap();
		result.put("teaProduct", teaProduct);
		result.put("teaUserAddr", teaUserAddr);
		logger.info("TeaOrderServiceImpl getOrderInfoBefore end");
		return BaseResult.success(result);
	}

	@Override
	public BaseResult saveOrder(TeaOrder order) {
		TeaProduct teaProduct = teaProductDao.selectByPrimaryKey(Long.valueOf(order.getProductid()));
		if(!(teaProduct.getTeaPrice().compareTo(order.getPerunit())==0)){
			return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		}
		//校验一下这个订单是否正确
		order.setId(UUIDUtil.getUUID());
		BigDecimal freight = order.getFreight();
		int conut = order.getConut();
		BigDecimal perunit = order.getPerunit();
		BigDecimal sum =freight.add(perunit.multiply(new BigDecimal(Integer.toString(conut))));
		if(!(sum.compareTo(order.getTotalunit())==0)){
			//不相等
			return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		}
		order.setId(UUIDUtil.getUUID());
		order.setOrderno(DateUtil.getNowTime().substring(0, 10).replaceAll("-", "")+Long.toString(System.currentTimeMillis()));
		order.setStatus(TeaContant.TEA_ORDER_STATUS_START);
		order.setAddtime(DateUtil.getNowTime());
		teaOrderDao.insert(order);
		return BaseResult.success(order);
	}

	@Override
	public BaseResult getOrderInfo(String orderId) {
		TeaOrder teaOrder = teaOrderDao.selectByPrimaryKey(orderId);
		TeaProduct teaProduct =teaProductDao.selectProOeder(Long.valueOf(teaOrder.getProductid()));
		TeaUserAddr teaUserAddr = teaUserAddrDao.selectByPrimaryKey(teaOrder.getAddrid());
		TeaFrontusers teaInfo = teaInfoService.getTeaInfoByUserId(teaOrder.getUserid());
		teaOrder.setTeaFrontusers(teaInfo);
		teaOrder.setTeaProduct(teaProduct);
		teaOrder.setTeaUserAddr(teaUserAddr);
		if(TeaContant.TEA_ORDER_STATUS_CANCLE.equals(teaOrder.getStatus())){
			teaOrder.setStatus("取消订单");
		}else if(TeaContant.TEA_ORDER_STATUS_START.equals(teaOrder.getStatus())){
			teaOrder.setStatus("未付款");
		}else if(TeaContant.TEA_ORDER_STATUS_PAY.equals(teaOrder.getStatus())){
			teaOrder.setStatus("未发货");
		}else if(TeaContant.TEA_ORDER_STATUS_SEND.equals(teaOrder.getStatus())){
			teaOrder.setStatus("已发货");
		}else if(TeaContant.TEA_ORDER_STATUS_RECIVE.equals(teaOrder.getStatus())){
			teaOrder.setStatus("已收货");
		}
		return BaseResult.success(teaOrder);
	}

	@Override
	public BaseResult cancleOrder(String orderId, String status) {
		logger.info("TeaOrderServiceImpl cancleOrder start");
		TeaOrder teaOrder = new TeaOrder();
		teaOrder.setId(orderId);
		if(TeaContant.TEA_ORDER_STATUS_RECIVE.equals(status)){
			teaOrder.setStatus(TeaContant.TEA_ORDER_STATUS_DELETE);
		}else if(TeaContant.TEA_ORDER_STATUS_SEND.equals(status)){
			teaOrder.setStatus(TeaContant.TEA_ORDER_STATUS_RECIVE);
		}else{
			teaOrder.setStatus(TeaContant.TEA_ORDER_STATUS_CANCLE);
		}
		teaOrderDao.updateByPrimaryKeySelective(teaOrder);
		logger.info("TeaOrderServiceImpl cancleOrder end");
		return BaseResult.success();
	}

	@Override
	public BaseResult getOrderListByUserId(Map param,TeaOrder order, PaginationInfo paginationInfo) {
		order.setPaginationInfo(paginationInfo);
		List<TeaOrder> teaOrderList = teaOrderDao.getOrderListForPage(order);
		if(!CollectionUtils.isEmpty(teaOrderList)){
			for (TeaOrder teaOrder : teaOrderList) {
				TeaProduct teaProduct =teaProductDao.selectProOeder(Long.valueOf(teaOrder.getProductid()));
				TeaUserAddr teaUserAddr = teaUserAddrDao.selectByPrimaryKey(teaOrder.getAddrid());
				TeaFrontusers teaInfo = teaInfoService.getTeaInfoByUserId(teaOrder.getUserid());
				teaOrder.setTeaFrontusers(teaInfo);
				teaOrder.setTeaProduct(teaProduct);
				teaOrder.setTeaUserAddr(teaUserAddr);
				if(TeaContant.TEA_ORDER_STATUS_CANCLE.equals(teaOrder.getStatus())){
					teaOrder.setStatus("取消订单");
				}else if(TeaContant.TEA_ORDER_STATUS_START.equals(teaOrder.getStatus())){
					teaOrder.setStatus("未付款");
				}else if(TeaContant.TEA_ORDER_STATUS_PAY.equals(teaOrder.getStatus())){
					teaOrder.setStatus("未发货");
				}else if(TeaContant.TEA_ORDER_STATUS_SEND.equals(teaOrder.getStatus())){
					teaOrder.setStatus("已发货");
				}else if(TeaContant.TEA_ORDER_STATUS_RECIVE.equals(teaOrder.getStatus())){
					teaOrder.setStatus("已收货");
				}
			}
		}
		Map<String, Object> retmap = new HashMap<String, Object>();
	    retmap.put("total", paginationInfo.getTotalRecord());
	    retmap.put("curPage", paginationInfo.getCurPage());
	    retmap.put("rows", teaOrderList);
		return BaseResult.success(retmap);
	}

	@Override
	public BaseResult sendOrder(TeaOrder teaOrder) {
		teaOrder.setStatus(TeaContant.TEA_ORDER_STATUS_SEND);
		teaOrderDao.updateByPrimaryKeySelective(teaOrder);
		return BaseResult.success();
	}

}
